
#include "LoadState.h"
#include "../../SGD Wrappers/CSGD_Direct3D.h"
#include "../../SGD Wrappers/CSGD_DirectInput.h"
#include "../../SGD Wrappers/CSGD_TextureManager.h"
#include "../../SGD Wrappers/CSGD_XAudio2.h"
#include "../../SGD Wrappers/SGD_String.h"
#include "../../SGD Wrappers/SGD_Math.h"
#include "EntityManager.h"
#include "Entity.h"
#include "Waypoint.h"
#include "Spawnpoint.h"
#include "Enemy.h"
#include "Bullet.h"
#include "Tower.h"
#include "ParticleManager.h"
#include "GameplayState.h"
#include "BitmapFont.h"
#include "CreditsState.h"
#include "MainMenuState.h"
#include "Game.h"
#include "BitmapFont.h"
#include <cassert>


//Singleton accessor
LoadState* LoadState::GetInstance(void)
{
	static LoadState s_Instance;
	return &s_Instance;
}


//Constructor
LoadState::LoadState(void)
{
	entityManager		= nullptr;
	particleManager		= nullptr;
	menuCursor			= 0;
}


//Destructor
LoadState::~LoadState(void)
{
}


//Enter
void LoadState::Enter(void)
{
	entityManager	= EntityManager::GetInstance();
	particleManager	= ParticleManager::GetInstance();
	menuCursor		= 0;

	/////////////////////////////////////////////////////////////////////////////////////
	////////    LEVEL TEST    ///////////////////////////////////////////////////////////
	TiXmlDocument tinyDoc;
	const char* fileName = "TestLevel.xml";
	if(tinyDoc.LoadFile(fileName))
	{
		TiXmlElement* document = tinyDoc.RootElement();
		if (document)
		{
			//Load tank
			//TiXmlElement* tank = document->FirstChildElement("Tank");
			//double tankX = 0.0;
			//tank->Attribute("X",&tankX);
			//double tankY = 0.0;
			//tank->Attribute("Y",&tankY);
			//Entity* newTank = entityManager->CreateEntity(ENTITY_TANK, (float)tankX, (float)tankY);
		}
	}
	/////////////////////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////////
}


//Exit
void LoadState::Exit(void)
{
	entityManager->Shutdown();
	particleManager->DestroyEmitters();
}


//Input
bool LoadState::Input(void)
{
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();
	static int lastMouseY = 0;

	//Exit to main menu
	if(pDI->KeyPressed(DIK_ESCAPE) == true)
	{
		Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
		return true;
	}

	if (pDI->KeyPressed(DIK_UP))
	{
		lastMouseY = pDI->MouseGetPosY();
		--menuCursor;
		if (menuCursor < 0)
			menuCursor = 4;
	}

	if (pDI->KeyPressed(DIK_DOWN))
	{
		lastMouseY = pDI->MouseGetPosY();
		++menuCursor;
		if (menuCursor > 4)
			menuCursor = 0;
	}

	int y = pDI->MouseGetPosY();
	if (y != lastMouseY)
	{
		lastMouseY = 0;
		if (y < 360) 
			menuCursor = 0;
		else if (y < 400)
			menuCursor = 1;
		else if (y < 440)
			menuCursor = 2;
		else if (y < 480)
			menuCursor = 3;
		else
			menuCursor = 4;
	}

	if (pDI->KeyPressed(DIK_RETURN) || pDI->MouseButtonPressed(0))
	{
		if (menuCursor == 0)
		{
			Game::GetInstance()->ChangeState(GameplayState::GetInstance());
			return true;
		}
		if (menuCursor == 1)
		{
		}
		if (menuCursor == 2)
		{
		}
		if (menuCursor == 3)
		{
		}
		if (menuCursor == 4)
		{
			Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
			return true;
		}
	}

	return true;
}


//Update
void LoadState::Update(float fElapsedTime)
{
}


//Render
void LoadState::Render(void)
{
	CBitmapFont* pFont = Game::GetInstance()->GetFont();

	pFont->Draw(_T("Save slots"), 300, 220, 1.0f, D3DCOLOR_ARGB(255,255,255,255));

	pFont->Draw(_T("New game"),          330, 320, 0.8f, D3DCOLOR_ARGB(255,255,255,255));
	pFont->Draw(_T("Slot 1 - Empty"),    330, 360, 0.8f, D3DCOLOR_ARGB(255,255,255,255));
	pFont->Draw(_T("Slot 2 - Empty"),    330, 400, 0.8f, D3DCOLOR_ARGB(255,255,255,255));
	pFont->Draw(_T("Slot 3 - Empty"),    330, 440, 0.8f, D3DCOLOR_ARGB(255,255,255,255));
	pFont->Draw(_T("Back to main menu"), 330, 480, 0.8f, D3DCOLOR_ARGB(255,255,255,255));

	if (menuCursor == 0)
		pFont->Draw(_T(">"), 290, 320, 0.8f, D3DCOLOR_ARGB(255,255,255,255));
	if (menuCursor == 1)
		pFont->Draw(_T(">"), 290, 360, 0.8f, D3DCOLOR_ARGB(255,255,255,255));
	if (menuCursor == 2)
		pFont->Draw(_T(">"), 290, 400, 0.8f, D3DCOLOR_ARGB(255,255,255,255));
	if (menuCursor == 3)
		pFont->Draw(_T(">"), 290, 440, 0.8f, D3DCOLOR_ARGB(255,255,255,255));
	if (menuCursor == 4)
		pFont->Draw(_T(">"), 290, 480, 0.8f, D3DCOLOR_ARGB(255,255,255,255));

}
